﻿using System.Collections;
using Castle.DynamicProxy;
using Com.Illuminati.Galileo.Net.Interceptor;

namespace Com.Illuminati.Galileo.Net
{
    public sealed class FubeiOpenApiFactory
    {
        static FubeiOpenApiFactory()
        {
        }

        private static readonly object Sync = new object();

        private static readonly Hashtable OpenApiImpl = new Hashtable();

        private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator();


        public static TInterface GetApi<TInterface>() where TInterface : class
        {
            lock (Sync)
            {
                var name = typeof(TInterface).FullName;
                if (OpenApiImpl[name] is TInterface api) 
                {
                    return api;
                }
                api = ProxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(ProxyGenerationOptions.Default, new FubeiOpenApiInterceptor());
                OpenApiImpl[name] = api;
                return api;
            }
        }
    }
}
